package com.pig4cloud.pigx.contract.lifecycle.started;

import com.pig4cloud.pigx.common.customize.redis.utils.RedisUtils;
import com.pig4cloud.pigx.common.customize.thread.SoftEndThread;
import com.pig4cloud.pigx.common.customize.utils.CommonUtil;
import com.pig4cloud.pigx.common.customize.utils.id.IdWorker;
import com.pig4cloud.pigx.contract.bo.CalculateKLineThreadParamBO;
import com.pig4cloud.pigx.contract.constant.ContractKLineConstants;
import com.pig4cloud.pigx.contract.mapper.ContractKLineMapper;
import com.pig4cloud.pigx.contract.mapper.ContractTradeMapper;
import com.pig4cloud.pigx.contract.thread.CalculateKLineThread;
import com.pig4cloud.pigx.contract.thread.repair.RepairKLine;
import com.pig4cloud.pigx.contract.utils.ContractUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

/**
 * contract模块线程启动器
 *
 * @since 2022/8/15 19:44
 */
@Order(0)
@Component
@AllArgsConstructor
@Data
public class ContractSoftEndThreadHandler implements ApplicationRunner {

    private final ContractTradeMapper contractTradeMapper;
    private final CommonUtil commonUtil;
    private final RedisUtils redisUtils;
    private final RedisTemplate redis;
    private final ContractUtils utils;
    private final MongoTemplate mongo;
    private final AmqpTemplate rabbit;
    private final IdWorker idWorker;
    private final ContractKLineMapper lineMapper;

    private final RepairKLine repairKLine;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        runCalculateKLineThread();
    }

    /**
     * 启动K线计算线程
     *
     * @since 2022/8/16 15:23
     */
    private void runCalculateKLineThread() {
        // 开启所有K线计算线程
        ContractKLineConstants.ENABLE_TIME_RANGE_TYPE_LIST.forEach(e -> {
            System.err.println("开启" + e.getType() + "线程");
            CalculateKLineThreadParamBO param = new CalculateKLineThreadParamBO(e.getType());
            CalculateKLineThread thread = new CalculateKLineThread(
                    contractTradeMapper,
                    param,
                    commonUtil,
                    redisUtils,
                    utils,
                    redis,
                    mongo,
                    rabbit,
                    idWorker,
                    lineMapper,
                    repairKLine
            );
            thread.start();
        });
    }

    @PreDestroy
    public void proDestroy() {
        SoftEndThread.runnable = false;
        while (SoftEndThread.threadMap.size() != 0) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
